class ModalManager {
    static show(content, options = {}) {
        const modal = document.createElement('div');
        modal.className = 'modal';
        
        // 如果content是选择器字符串
        if (typeof content === 'string') {
            const template = document.querySelector(content);
            if (template) {
                const modalContent = template.content.cloneNode(true).firstElementChild;
                modalContent.style.display = 'block';
                modal.appendChild(modalContent);
            }
        } 
        // 如果content是DOM元素
        else if (content instanceof Element) {
            const modalContent = content.cloneNode(true);
            modalContent.style.display = 'block';
            modal.appendChild(modalContent);
        }

        // 添加关闭按钮
        if (options.showClose !== false) {
            const closeBtn = modal.querySelector('.close-btn') || document.createElement('button');
            closeBtn.className = 'close-btn';
            closeBtn.innerHTML = '×';
            closeBtn.onclick = () => this.hide(modal);
            if (!modal.querySelector('.close-btn')) {
                modal.firstElementChild.appendChild(closeBtn);
            }
        }

        // 点击背景关闭
        if (options.closeOnClickOutside !== false) {
            modal.addEventListener('click', (e) => {
                if (e.target === modal) {
                    this.hide(modal);
                }
            });
        }

        // 自定义事件处理
        if (options.onShow) {
            options.onShow(modal.firstElementChild);
        }

        document.body.appendChild(modal);
        return modal;
    }

    static hide(modal) {
        if (typeof modal === 'string') {
            modal = document.querySelector(modal);
        }
        if (modal) {
            modal.remove();
        }
    }

    static showMessage(options = {}) {
        return new Promise((resolve) => {
            this.show('#message-modal', {
                showClose: false,
                closeOnClickOutside: false,
                onShow: (content) => {
                    if (!content) return;
                    
                    const title = content.querySelector('.message-title');
                    const text = content.querySelector('.message-text');
                    const confirmBtn = content.querySelector('.confirm-btn');
                    const cancelBtn = content.querySelector('.cancel-btn');
                    
                    if (title) title.textContent = options.title || '提示';
                    if (text) text.textContent = options.message;
                    
                    if (options.showCancel && cancelBtn) {
                        cancelBtn.style.display = 'block';
                        cancelBtn.textContent = options.cancelText || '取消';
                        cancelBtn.onclick = () => {
                            this.hide('.modal');
                            resolve(false);
                        };
                    }
                    
                    if (confirmBtn) {
                        confirmBtn.textContent = options.confirmText || '确定';
                        confirmBtn.onclick = () => {
                            this.hide('.modal');
                            resolve(true);
                        };
                    }
                }
            });
        });
    }
} 